package com.battle.service.repository.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.battle.pojo.entity.Organization;
import com.battle.pojo.entity.User;
import com.battle.pojo.entity.UserOrganizationMp;
import com.battle.pojo.vo.organization.GetOrganizationTreeListVo;
import com.battle.service.mapper.OrganizationMapper;
import com.battle.service.repository.in.OrganizationRepository;
import nirvana.core.repository.Repository;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
    import com.battle.pojo.dto.organization.*;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* 【描 述】：
* 【环 境】：J2SE
* 【详 细】：
*
*/
@Service
public class OrganizationRepositoryImpl extends Repository< OrganizationMapper , Organization> implements OrganizationRepository {

private final OrganizationMapper mapper;

public OrganizationRepositoryImpl(OrganizationMapper mapper) {
this.mapper = mapper;
}

    /**
    * 搜索组织架构
    * @param dto
    * @return
    */
    @Override
    public Page< Organization > selectOrganization(SelectOrganizationDto dto){

    return mapper.selectOrganization(dto);
    }

    @Override
    public Map<Long, Organization> getMap(List<Long> ids) {
        Map<Long, Organization> map = new HashMap<>();

        if (CollectionUtil.isNotEmpty(ids)) {
            List<Organization> userList =listByIds(ids);
            if (CollectionUtil.isNotEmpty(userList)) {
                map = userList.stream().collect(Collectors.toMap(Organization::getId, pojo -> pojo));
            }
        }
        return map;
    }

    @Override
    public Map<UserOrganizationMp, Organization> getUserOrganizationMap(List<UserOrganizationMp> ids) {
        Map<UserOrganizationMp, Organization> map = new HashMap<>();

        if (CollectionUtil.isNotEmpty(ids)) {
            List<Organization> organizationList = listByIds(ids.stream().map(UserOrganizationMp::getOrganizationId).collect(Collectors.toList()));
            if (CollectionUtil.isNotEmpty(organizationList)) {
                for (UserOrganizationMp userOrganizationMp : ids) {
                    for (Organization organization : organizationList) {
                        if (userOrganizationMp.getOrganizationId().equals(organization.getId())) {
                            map.put(userOrganizationMp, organization);
                        }
                    }
                }
            }
        }
        return map;
    }

    /**
     * 根据当前id查出所有的层级，并且拼接
     * @param id
     * @return
     */
    @Override
    public String selectPathById(Long id,String path) {
        Organization organization = getById(id);
        if (organization!=null){
            if (path==null){
                path=organization.getId().toString();
            }else{
                path=path+","+organization.getId().toString();
            }
            if (organization.getParentId()!=0){
                return selectPathById(organization.getParentId(), path);
            }else{
                return path;
            }
        } else {
            return path;
        }
    }

    @Override
    public Organization selectByName(String string) {
        return mapper.selectOne(new LambdaQueryWrapper<Organization>()
                .eq(Organization::getName, string)
                .eq(Organization::getIsDelete, 0));
    }

    @Override
    public List<Long> getOrganizationIdList(Long organizationId) {
        List<Organization> systemOrganizationList = mapper.selectList(new LambdaQueryWrapper<Organization>()
                .eq(Organization::getIsDelete, 0));
        List<GetOrganizationTreeListVo> voList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(systemOrganizationList)){
            for (Organization organization : systemOrganizationList) {
                GetOrganizationTreeListVo vo=new GetOrganizationTreeListVo();
                BeanUtils.copyProperties(organization,vo);
                voList.add(vo);
            }

            Map<Long, List<GetOrganizationTreeListVo>> departmentMap = voList.stream().collect(Collectors.groupingBy(GetOrganizationTreeListVo::getParentId));
            for (GetOrganizationTreeListVo nodeVo : voList) {
                List<GetOrganizationTreeListVo> vos = departmentMap.get(nodeVo.getId());
                nodeVo.setChildList(vos);
            }

            List<GetOrganizationTreeListVo> vos = voList.stream().filter(r -> r.getId().equals(organizationId)).collect(Collectors.toList());

            List<GetOrganizationTreeListVo> list = treeToList(vos, new ArrayList<>());
            return list.stream()
                    .map(GetOrganizationTreeListVo::getId)
                    .toList();
        }
        return null;
    }

    public List<GetOrganizationTreeListVo> treeToList(List<GetOrganizationTreeListVo> tree, List<GetOrganizationTreeListVo> list) {
        for (GetOrganizationTreeListVo vo : tree) {
            list.add(vo);
            if (vo.getChildList() != null) {
                treeToList(vo.getChildList(), list);
            }
        }
        return list;
    }

}